"""
configures platform-dependent default parameters
"""
import sys
import os
from .logging import loginfo, logwarning
from . import chains
from .core import Namespace


params = Namespace()


def getenvlist(name):
    return tuple(os.environ.get(name, "").split(env_list_splitter))


def conf_win32():
    params.c_toolchain = chains.msvc.CToolchain
    params.cpp_toolchain = chains.msvc.CppToolchain
    params.c_include_path = getenvlist("INCLUDE")
    params.cpp_include_path = getenvlist("INCLUDE")
    params.c_lib_path = getenvlist("LIB")
    params.cpp_lib_path = getenvlist("LIB")


def conf_posix():
    params.c_toolchain = chains.gcc.CToolchain
    params.cpp_toolchain = chains.gcc.CppToolchain
    params.c_include_path = getenvlist("C_INCLUDE_PATH") + getenvlist("CPATH")
    params.cpp_include_path = getenvlist("CPLUS_INCLUDE_PATH")
    params.c_lib_path = getenvlist("LIBRARY_PATH")
    params.cpp_lib_path = getenvlist("LIBRARY_PATH")
    #
    # in accordance with http://www.decf.berkeley.edu/help/unix/searchpath.html
    #
    extra_includes = []
    extra_libs = []
    for name in getenvlist("PATH"):
        inc = os.path.join(os.path.dirname(name), "include")
        lib = os.path.join(os.path.dirname(name), "lib")
        if os.path.isdir(inc):
            extra_includes.append(inc)
        if os.path.isdir(lib):
            extra_includes.append(lib)
    extra_includes = tuple(extra_includes)
    extra_libs = tuple(extra_libs)
    params.c_lib_path += extra_libs
    params.c_include_path += extra_includes
    params.cpp_include_path += extra_includes
    params.c_lib_path += extra_libs
    params.cpp_lib_path += extra_libs




if sys.platform == "win32":
    env_list_splitter = ";"
    conf_win32()
elif sys.platform == "posix":
    env_list_splitter = ":"
    conf_posix()
else:
    logwarning("MAIN", "can't configure platform %r", sys.platform)


